package com.qingxin.wuye.repertory.service;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.repertory.entity.Gi;
import com.qingxin.wuye.repertory.entity.RukuDetail;
import com.qingxin.wuye.repertory.entity.RukuDocket;

@Service
public class RukuDetailService extends BaseService<RukuDetail, String>{
	@Autowired
	private GiService giService;
	@Autowired
	private RukuDocketService rukuDocketService;
	@Autowired
	private ChukuDetailService chukuDetailService;
	public Pager list(String searchname,String docketid, String repertoryid, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.repertory.entity.RukuDetail(" +
				"r.detailid,r.docketid,r.repertoryid,r.resid, r.resCode, r.resName, r.num, r.price, r.amount, r.remark)" +
				" from RukuDetail r" +
				" where 1=1");
		
		if (StringUtils.isNotEmpty(docketid)) {
			querier.append(" and r.docketid in (select t.docketid from RukuDocket t where t.docketid =:docketid) ");
			querier.setParam("docketid",docketid);
		}
			
		if (StringUtils.isNotEmpty(repertoryid)) {
			querier.append(" and r.repertoryid=:repertoryid");
			querier.setParam("repertoryid",repertoryid);
		}

		if(StringUtils.isNotEmpty(searchname)){
			searchname = searchname.replace("'", "");
			querier.append(" and r,resName like :resName ");
			querier.setParam("resName", "%"+searchname+"%");
		}
		querier.append(pageParam.getOrderby(" order by r.createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 添加
	 * @param rukuDetail
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(RukuDetail rukuDetail, LoginUser loginUser, HttpServletRequest request){
		Object obj = getByHql("select detailid from RukuDetail where resid=? and docketid=?", rukuDetail.getResid(),rukuDetail.getDocketid());
		if (obj != null) {
			return new SysError("该商品已存在明细");
		}
		rukuDetail.setCreateTime(new Date());
		rukuDetail.setCreateBy(loginUser.getLogin_name());
		this.save(rukuDetail);

		//如果此仓库中此商品还没有 库存数据 添加库存
		Object object = giService.getByHql("select giid from Gi where repertoryid=? and resid=?", rukuDetail.getRepertoryid(),rukuDetail.getResid());
		if (object == null) {
			Gi gi = new Gi();
			gi.setResid(rukuDetail.getResid());
			RukuDocket rukuDocket = rukuDocketService.get(rukuDetail.getDocketid());
			gi.setRepertoryid(rukuDocket.getRepertoryid());
			gi.setGinum(rukuDetail.getNum());
			gi.setInNum(rukuDetail.getNum());
			giService.add(gi, loginUser, request);
		}else {
			//更新库存
			Gi gi = (Gi) giService.getByHql("from Gi where repertoryid=? and resid=?", rukuDetail.getRepertoryid(),rukuDetail.getResid());
			gi.setGinum(rukuDetail.getNum() + gi.getGinum());
			gi.setInNum(rukuDetail.getNum() + gi.getInNum());
			giService.update(gi, loginUser, request);
		}
		
		return null;
	}
	
	/**
	 * 更新
	 * @param rukuDetail
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(RukuDetail rukuDetail, LoginUser loginUser, HttpServletRequest request){
		//此前的明细 
		RukuDetail old = getSafeEntity(rukuDetail.getDetailid());
		Object objectold = chukuDetailService.getByHql("select detailid from ChukuDetail where resid = ? and repertoryid = ?", old.getResid(),old.getRepertoryid());
		if (objectold != null) {
			return new SysError("修改前的物品存在出库明细,不可修改");
		}
		Object object = chukuDetailService.getByHql("select detailid from ChukuDetail where resid = ? and repertoryid = ?", rukuDetail.getResid(),rukuDetail.getRepertoryid());
		if (object != null) {
			return new SysError("修改后的物品存在出库明细,不可修改");
		}
		Object obj = getByHql("select detailid from RukuDetail where detailid<>? and resid=? and docketid=?",rukuDetail.getDetailid(), rukuDetail.getResid(),rukuDetail.getDocketid());
		if (obj != null) {
			return new SysError("该商品已存在明细");
		}else {
			//如果更新的时候更新新了商品
			if (!rukuDetail.getResid().equals(old.getResid())) {
			/*	//更新前的商品数量操作
				Gi giOld = (Gi) giService.getByHql("from Gi where repertoryid=? and resid=?", old.getRepertoryid(),old.getResid());
				if (giOld != null) {
					giService.delete(old.getResid(),old.getRepertoryid(), loginUser, request);
				}*/
				
				/*Gi gi = (Gi) giService.getByHql("from Gi where repertoryid=? and resid=?", rukuDetail.getRepertoryid(),rukuDetail.getResid());
				//更新的商品存在库存
				if (gi != null) {
					//变更后商品的明细
					RukuDetail oldNew = (RukuDetail) getByHql("from RukuDetail where resid = ? and repertoryid = ?", rukuDetail.getResid(),rukuDetail.getRepertoryid());
					if (oldNew != null) {
						gi.setResid(rukuDetail.getResid());
						RukuDocket rukuDocket = rukuDocketService.get(rukuDetail.getDocketid());
						gi.setRepertoryid(rukuDocket.getRepertoryid());
						
						//修改的时候  入库量 不能 低于出库量
						if (gi.getOutNum() != null) {
							if (rukuDetail.getNum() < gi.getOutNum()) {
								return new SysError("入库量不能低于现有出库量");
							}
						}
						
						//库存数量变更操作   若修改后的数量小于 此前的数量  为负数  反之为正数
						Integer editNum = 0;
						editNum = rukuDetail.getNum()-oldNew.getNum();
						gi.setGinum(gi.getGinum() + editNum);
						gi.setInNum(gi.getInNum() + editNum);
						giService.update(gi, loginUser, request);
					}
				}else{//更新的商品 没有明细
					Gi giNew = new Gi();
					giNew.setResid(rukuDetail.getResid());
					RukuDocket rukuDocket = rukuDocketService.get(rukuDetail.getDocketid());
					giNew.setRepertoryid(rukuDocket.getRepertoryid());
					giNew.setGinum(rukuDetail.getNum());
					giNew.setInNum(rukuDetail.getNum());
					giService.add(giNew, loginUser, request);
				}*/
				Gi giOld = (Gi) giService.getByHql("from Gi where repertoryid=? and resid=?", old.getRepertoryid(),old.getResid());
				if (giOld != null) {
					giService.delete(old.getResid(),old.getRepertoryid(), loginUser, request);
				}
				
				Gi giNew = new Gi();
				giNew.setResid(rukuDetail.getResid());
				RukuDocket rukuDocket = rukuDocketService.get(rukuDetail.getDocketid());
				giNew.setRepertoryid(rukuDocket.getRepertoryid());
				giNew.setGinum(rukuDetail.getNum());
				giNew.setInNum(rukuDetail.getNum());
				giService.add(giNew, loginUser, request);
				
			}else {
				//更新库存
				Gi gi = (Gi) giService.getByHql("from Gi where repertoryid=? and resid=?", rukuDetail.getRepertoryid(),rukuDetail.getResid());
				if (gi != null) {
					//修改的时候  入库量 不能 低于出库量
					if (gi.getOutNum() != null) {
						if (rukuDetail.getNum() < gi.getOutNum()) {
							return new SysError("入库量不能低于现有出库量");
						}
					}
					//库存数量变更操作   若修改后的数量小于 此前的数量  为负数  反之为正数
					Integer editNum = 0;
					if (rukuDetail.getNum() > old.getNum()) {
						editNum = rukuDetail.getNum() - old.getNum();
						gi.setGinum(gi.getGinum() + editNum);
						gi.setInNum(gi.getInNum() + editNum);
						System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>库存增加"+editNum);
					}else if (rukuDetail.getNum() < old.getNum()) {
						editNum = old.getNum() - rukuDetail.getNum();
						gi.setGinum(gi.getGinum() - editNum);
						gi.setInNum(gi.getInNum() - editNum);
						System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>库存减少"+editNum);
					}
					giService.update(gi, loginUser, request);
				}
			}
			
			String fields = "docketid,resid,resCode,resName,num,price,amount,remark";
			BeanUtil.setTargetFields(old, rukuDetail, fields, true);
			Updater<RukuDetail> updater = new Updater<>(old, fields);
			this.update(updater);
		}
		return null;
	}
	
	/**
	 * 删除操作
	 * @param detailid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Object delete(String[] detailid, LoginUser loginUser, HttpServletRequest request){
		for(String id : detailid){
			RukuDetail rukuDetail = get(id);
			if(rukuDetail==null){
				continue;
			}
			/*List<RukuDetail> details = listByHql("from RukuDetail where resid = ? and repertoryid = ?", rukuDetail.getResid(),rukuDetail.getRepertoryid());
			Gi gi = (Gi) giService.getByHql("from Gi where repertoryid=? and resid=?", rukuDetail.getRepertoryid(),rukuDetail.getResid());
			if (details.size() > 1) {
				if (gi != null) {
					//修改的时候  入库量 不能 低于出库量
					if (gi.getOutNum() != null) {
						if ((gi.getGinum() - rukuDetail.getNum()) < gi.getOutNum()) {
							return new SysError("入库量不能低于现有出库量");
						}
					}
					gi.setGinum(gi.getGinum() - rukuDetail.getNum());
					gi.setInNum(gi.getInNum() - rukuDetail.getNum());
					giService.update(gi, loginUser, request);
				}
				System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>删除>>>减少库存");
			}else{
				
				if (gi != null) {
					//修改的时候  入库量 不能 低于出库量
					if (gi.getOutNum() != null) {
						if ((gi.getGinum() - rukuDetail.getNum()) < gi.getOutNum()) {
							return new SysError("入库量不能低于现有出库量");
						}
					}
				}
				giService.delete(rukuDetail.getResid(),rukuDetail.getRepertoryid(), loginUser, request);
				System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>删除>>>删除库存");
			}*/
			Object objectold = chukuDetailService.getByHql("select detailid from ChukuDetail where resid = ? and repertoryid = ?", rukuDetail.getResid(),rukuDetail.getRepertoryid());
			if (objectold != null) {
				return new SysError("该物品存在出库明细");
			}
			
			List<RukuDetail> details = listByHql("from RukuDetail where resid = ? and repertoryid = ?", rukuDetail.getResid(),rukuDetail.getRepertoryid());
			if (details.size() > 1) {
				Gi gi = (Gi) giService.getByHql("from Gi where repertoryid=? and resid=?", rukuDetail.getRepertoryid(),rukuDetail.getResid());
				gi.setGinum(gi.getGinum() - rukuDetail.getNum());
				gi.setInNum(gi.getInNum() - rukuDetail.getNum());
				giService.update(gi, loginUser, request);
			}else {
				giService.delete(rukuDetail.getResid(),rukuDetail.getRepertoryid(), loginUser, request);
			}
			this.delete(rukuDetail);
		}
		return null;
	}
	
	
	/**
	 * 判断此类商品是否添加过明细
	 * @param resid
	 * @param detailid
	 * @return
	 */
	public boolean isResDuplicate(String resid, String detailid){
		if(StringUtils.isEmpty(detailid)){
			Object obj = getByHql("select detailid from RukuDetail where resid=? ", resid);
			return obj!=null;
		}else {
			Object obj = getByHql("select detailid from ResType where detailid<>? and resid=? ", detailid, resid);
			return obj!=null;
		}
	}
	
}
